
package suncertify.middle;
import java.io.IOException;
import suncertify.shared.ServerBroker;

//~--- JDK imports ------------------------------------------------------------

import java.net.MalformedURLException;

import java.rmi.Naming;
import java.rmi.NotBoundException;
import java.rmi.RemoteException;
import java.rmi.registry.Registry;

import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;


/**
 *
 * @author jshah
 * This is the RMI implementation of the <code>ClientBroker</code> Interface, used when the user selects rmi mode
 */
public class RemoteClientBroker implements ClientBroker {

    String       rmUrl ;
    ServerBroker broker;
    Registry r ;

    /**
     * default constructor
     * @param rmiUrl The String which encloses the location to bind to the remote object url is of the form http://hostname/url:portNo
     */
    public RemoteClientBroker(String rmiUrl) {
        try {
            this.rmUrl = rmiUrl;
            Logger.getLogger(RemoteClientBroker.class.getName()).log(Level.INFO,this.rmUrl);
            broker = (ServerBroker)Naming.lookup(rmUrl);
        } catch (NotBoundException ex) {
            Logger.getLogger(RemoteClientBroker.class.getName()).log(Level.SEVERE, null, ex);
        }
        catch (MalformedURLException ex) {
            Logger.getLogger(RemoteClientBroker.class.getName()).log(Level.SEVERE, null, ex);
        }
        catch (RemoteException ex) {
            Logger.getLogger(RemoteClientBroker.class.getName()).log(Level.SEVERE, null, ex);
        }
        catch(IOException ex){
            Logger.getLogger(RemoteClientBroker.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public boolean setDbPath(String path){
       //not required for RMI mode
       return false;
    }

    public boolean setDbPath(){
        try {
            boolean dbset = broker.setDbPath();
            return dbset;
        } catch (RemoteException ex) {
            Logger.getLogger(RemoteClientBroker.class.getName()).log(Level.SEVERE, "ERROR LOCATING DBPATH AND INSTANTIATING SERVER", ex);
            return false;
        }
    }
    public ArrayList<String[]> searchRecords(String name, String Location) {
        try {
            return broker.searchRecords(name, Location);
        } catch (RemoteException ex) {
            Logger.getLogger(RemoteClientBroker.class.getName()).log(Level.SEVERE, null, ex);

            return null;
        }
    }

    public String book(int recNo, String customerID) {
        try {
            return broker.book(recNo, customerID);
        } catch (RemoteException ex) {
            Logger.getLogger(RemoteClientBroker.class.getName()).log(Level.SEVERE, null, ex);

            return "A SERVER SIDE ERROR";
        }
    }

    public String unbook(int recNo) {
        try {
            return broker.unbook(recNo);
        } catch (RemoteException ex) {
            Logger.getLogger(RemoteClientBroker.class.getName()).log(Level.SEVERE, null, ex);

            return "A SERVER SIDE ERROR";
        }
    }

    public String[] read(int recNo) {
        try {
            return broker.read(recNo);
        } catch (RemoteException ex) {
            Logger.getLogger(RemoteClientBroker.class.getName()).log(Level.SEVERE, null, ex);

            return null;
        }
    }
}


